<template>
  <BasicModal
    v-bind="$attrs"
    :width="modalWidth"
    @register="registerModal"
    :title="getTitle"
    @ok="handleOK"
  >
    <BasicForm @register="registerForm">
      <template #action>
        <Button type="primary" @click="onSearch">生成提示词</Button>
      </template>
    </BasicForm>

    <div v-if="tips">
      <div style="margin: 10px 0 0 10px; font-weight: bold">提示词</div>
      <div style="margin-left: 20px; margin-top: 10px">
        <a-textarea v-model:value="tips" :autoSize="{ minRows: 5, maxRows: 6 }" />
      </div>
    </div>
  </BasicModal>
</template>

<script lang="ts" setup>
  import { onMounted } from 'vue';
  import { Steps, Card, FormItem, Button, message, InputNumber } from 'ant-design-vue';
  import Icon from '@/components/Icon/Icon.vue';
  import { ref, computed, unref } from 'vue';
  import { BasicModal, useModalInner } from '@/components/Modal';
  import { BasicForm, useForm } from '@/components/Form';
  import axios from 'axios';
  import { matchCategory } from '@/api/demo/InformationMatching'; // 假设 matchCategory 接口在这里
  import { accountFormSchema } from './account.data';
  import { AddCreateMatchGeneralTemplateAPI } from '@/api/demo/template';
  import { useI18n } from "@/hooks/web/useI18n";

  const { t } = useI18n();
  const value = ref(0);
  const emit = defineEmits(['success']);
  defineOptions({ name: 'AccountModal' });
  const isUpdate = ref(true);
  const rowId = ref('');
  const modalWidth = ref('40%');
  const tips = ref('');
  const [
    registerForm,
    { resetFields, setFieldsValue, updateSchema, getFieldsValue: getFieldsValuePosi },
  ] = useForm({
    labelWidth: 100,
    baseColProps: { span: 24 },
    schemas: accountFormSchema,
    showActionButtonGroup: false,
  });

  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    await resetFields();
    setModalProps({ confirmLoading: false });
    isUpdate.value = !!data?.isUpdate;

    if (unref(isUpdate)) {
      rowId.value = data.record.id;
      setFieldsValue({
        ...data.record,
      });
    }

    const categoryData = await matchCategory();
    // 更新 accountFormSchema 中的 MatchingType 值
    updateSchema({
      field: 'category',
      componentProps: { options: categoryData.data },
    });
  });
  const getTitle = computed(() => (!unref(isUpdate) ? t('sys.configuration.add') : t('sys.configuration.edit')));

  async function handleOK() {
    let params = {};
    params = {
      ...getFieldsValuePosi(),
      type: 'match',
      callword: tips.value,
    };
    const data = await AddCreateMatchGeneralTemplateAPI(params);
    emit('success', data); // 触发 success 事件
    closeModal();
  }

  async function fetchKeytips(params) {
    //本地 http://192.168.0.109:6001
    //线上 http://8.136.118.105:6001
    try {
      const response = await axios.get('http://8.136.118.105:6001/getTips', {
        params,
      });

      tips.value = response.data.data;

      return response.data;
    } catch (error) {
      message.error('生成提示词失败');
      return null;
    }
  }

  async function onSearch(value) {
    let Data = { ...getFieldsValuePosi() };

    let params = {
      content: Data.remark,
      title: Data.title,
    };
    const data = await fetchKeytips(params);
    if (data) {
      message.success('生成提示词成功');
    }
  }
</script>
